package com.ccxc.question.exam.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ccxc.question.exam.dao.OperationDAO;
import com.ccxc.question.exam.dto.OperationDTO;

@Service
@Transactional
public class AnswerService {

    @Autowired
    private OperationDAO operationDao;

    public String contrast(OperationDTO operationDto) {
        int i = 1;
        //判断提交答案是否正确
        if (operationDao.contrast(operationDto.getQueid()).equals(operationDto.getAnswer())) {

            operationDao.record(operationDto.getQueid(), operationDto.getAnswer(), i, operationDto.getUserid(), operationDao.contrast2(operationDto.getQueid()), operationDao.contrast(operationDto.getQueid()));

            //判断每日答题表是否存在
            if (operationDao.judge(operationDto.getUserid())) {

                operationDao.bonus_points(operationDto.getUserid());

            } else {

                operationDao.build(operationDto.getQueid(), operationDto.getUserid());
                operationDao.bonus_points(operationDto.getUserid());
            }

            //判断答题汇总表是否存在
            if (operationDao.judge2(operationDto.getUserid())) {
                operationDao.correct(operationDto.getUserid());
            } else {
                operationDao.build2(operationDto.getUserid());
                operationDao.correct(operationDto.getUserid());
            }
            return "正确";
        } //答题错误
        else {

            i = -3 / 5;

            operationDao.record(operationDto.getQueid(), operationDto.getAnswer(), i, operationDto.getUserid(), operationDao.contrast2(operationDto.getQueid()), operationDao.contrast(operationDto.getQueid()));
            //判断每日答题表是否存在
            if (operationDao.judge(operationDto.getUserid())) {
                if (operationDao.judgescore(operationDto.getUserid())) {
                    operationDao.points2
                            (operationDto.getUserid());
                } else {
                    operationDao.points(operationDto.getUserid());
                }
            } else {
                operationDao.build(operationDto.getQueid(), operationDto.getUserid());
                operationDao.points(operationDto.getUserid());
            }

            //判断答题汇总表是否存在
            if (operationDao.judge2(operationDto.getUserid())) {
                if (operationDao.judgescore(operationDto.getUserid())) {
                    operationDao.error(operationDto.getUserid());
                } else {
                    operationDao.error2(operationDto.getUserid());
                }
            } else {
                operationDao.build2(operationDto.getUserid());
                operationDao.error(operationDto.getUserid());
            }
        }


        return "错误";


    }


    public void contrast(String userid) {
        if (operationDao.judge2(userid)) {

        } else {
            operationDao.build2(userid);
        }
    }

}
